package com.ymttest.database.sqlwapper.hunter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.hunter.HunterFans;
import com.ymttest.database.model.hunter.HunterRelation;

public class HunterFansWrapper {
	
	SqlManager manager;

    public HunterFansWrapper() {
        manager = new SqlManager("hunter");
    }
    
    @SuppressWarnings("unchecked")
	public HunterFans selectByPrimaryKey(Long userId) {
    	
    	HunterFans hunterFans = new HunterFans();
    	List<HunterFans> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.hunter.HunterFansMapper.selectByPrimaryKey",
                        userId);
        manager.commit();
        
		if (result.size() > 0) {
			hunterFans = result.get(0);
		}
		
        return hunterFans;
    }
    
	// 获取有效的粉丝数
	@SuppressWarnings("unchecked")
	public List<HunterFans> selectFansByHunterId(Long hunterId) {

		List<HunterFans> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterFansMapper.selectFansByHunterId",
						hunterId);
		manager.commit();

		if (result.size() == 0) {
			return null;
		}

		return result;
	}
	
	// 通过userId和hunterId获取粉丝
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterFans selectFansByHunterIdAndUserId(Long hunterId, Long userId) {

		HunterFans hunterFans = new HunterFans();
		
		Map m = new HashMap<String, String>();
    	
		m.put("hunter_id", hunterId);
		m.put("user_id", userId);
		
    	List<HunterFans> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.hunter.HunterFansMapper.selectFansByHunterIdAndUserId",
                        m);
        manager.commit();
        
		if (result.size() > 0) {
			hunterFans = result.get(0);
		}
		
        return hunterFans;
	}

	// 通过粉丝名称和hunterId获取粉丝
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterFans selectFansByHunterIdAndFansName(Long hunterId, String userName) {

		HunterFans hunterFans = new HunterFans();

		Map m = new HashMap<String, String>();

		m.put("hunter_id", hunterId);
		m.put("user_name", userName);

		List<HunterFans> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterFansMapper.selectFansByHunterIdAndFansName",
						m);
		manager.commit();

		if (result.size() > 0) {
			hunterFans = result.get(0);
		}

		return hunterFans;
	}
   
    public int insert(HunterRelation hunterRelation) {
		
    	int count = manager
				.insert("com.ymttest.database.mapping.hunter.HunterRelationMapper.insert",
						hunterRelation);
    	manager.commit();
		return count;
    }
    
    public static void main(String[] args) {
    	HunterFansWrapper test = new HunterFansWrapper();
    	
    	/*HunterRelation relation = new HunterRelation();
    	relation.setHunter_id(10001L);
    	relation.setHunter_name("mybatis");
    	relation.setHunter_level(0);
    	relation.setParent_hunter_id(0L);
    	relation.setStatus(0);
    	relation.setAdd_time(new Date());
    	relation.setUpdate_time(new Date());
    	
    	System.out.println(test.insert(relation));*/
    	
        System.out.println(test.selectFansByHunterIdAndFansName(1234L, "autotest_Dw8x9EvLqS").getUser_id());
        // test.insertYmtCouponSetting(3,11);
        // Logger.debug(test.getLastYmtCouponSettingId());


    }
}
